package com.dou.active;

import com.alibaba.fastjson.JSONObject;
import com.dou.dto.Constant;
import com.dou.dto.DtoUtil;
import com.dou.dto.RedisUtil;
import com.dou.entity.QiangGou;
import com.dou.entity.SeckillGoods;
import com.dou.entity.SeckillOrderStemp;
import com.dou.service.SeckillOrderStempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class ActiveHandle {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SeckillOrderStempService seckillOrderTempService;
    @JmsListener(destination = "que")
    public void que(String msg){
        System.out.println("点对点发送数据"+msg);
    }
    @JmsListener(destination = "ding")
    public void dingtalk(String ding){
        System.out.println(ding);
    }
    @JmsListener(destination = Constant.MQ.QIANGGOU)
    public void qiangGou(QiangGou gg){
        int gId = gg.getGid();
        int sId = gg.getSid();
        int uId = gg.getUid();
        while (redisUtil.lock(Constant.Seckill.SECKILL_LOCK+gId,30L)){
            try {
                //休眠当前线程
                Thread.sleep(30);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
        //是否有库存
        String gJson = redisUtil.getStr(Constant.Seckill.SECKILL_GOODS+sId+":"+gId);
        if (gJson == null || gJson.equals("")){
            redisUtil.unlock(Constant.Seckill.SECKILL_LOCK+gId);
            //异步操作 将对应数据放到redis里
            //4001代表没有库存了
            redisUtil.setStr(Constant.MQ.QGMSG+sId+":"+uId+":"+gId,"4001");
            return;
        }
        SeckillGoods goods = JSONObject.toJavaObject(JSONObject.parseObject(gJson),SeckillGoods.class);
        if(goods.getSstock()<1){
            redisUtil.unlock(Constant.Seckill.SECKILL_LOCK+gId);
            //4000代表商品抢完了
            redisUtil.setStr(Constant.MQ.QGMSG+sId+":"+uId+":"+gId,"4000");
            return;
        }
        //查询用户是否购买 查询订单表？订单表中有其他数据，查询不便，创建临时表存储
        SeckillOrderStemp sot = new SeckillOrderStemp();
        sot.setUid(uId);
        sot.setSid(sId);
        sot.setGid(gId);
        int count = seckillOrderTempService.getCount(sot);
        if(count != 0){
            redisUtil.unlock(Constant.Seckill.SECKILL_LOCK+gId);
            //4002只能抢购一次
            redisUtil.setStr(Constant.MQ.QGMSG+sId+":"+uId+":"+gId,"4002");
            return;
        }
        //开始抢购
        //修改库存
        int sstock=goods.getSstock();
        System.out.println(sstock);
        goods.setSstock(sstock-1);
        redisUtil.setStr(Constant.Seckill.SECKILL_GOODS+":"+gId,JSONObject.toJSONString(goods));
        //新增秒杀临时表中记录
        sot.setStatus(0);
        seckillOrderTempService.insert(sot);
        //创建订单
        redisUtil.unlock(Constant.Seckill.SECKILL_LOCK + gId);
        /* 1:代表抢购成功*/
        redisUtil.setStr(Constant.MQ.QGMSG+sId+":"+uId+":"+gId,"1");
    }
}
